﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using wpf_read_config;
using mywinform.util;
using mywinform.bean;

namespace mywinform.ui
{
    public partial class PowerNameSetting : Form
    {
        public PowerNameSetting()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {

        }

        private void form_load(object sender, EventArgs e)
        {
            //ConfigLoad config = new ConfigLoad("config.txt");
            //string name1=config.GetStringValue(string.Format("name_{0}", 1));
            //Console.WriteLine();
            //QueryAllCourse();
            query2();
            int count=dataGridView1.Rows.Count;
            dataGridView1.Rows[count - 1].Cells[1].ReadOnly = false;
            dataGridView1.Rows[count - 1].Cells[2].ReadOnly = false;
        }
        HashSet<int> set = new HashSet<int>();
        List<PowerNameSettingPojo> list = new List<PowerNameSettingPojo>();
        private void query2()
        {
            string sql = "select * from st_power_name";
            DataSet ds=DbUtil.create().Query(sql);
            DataRowCollection drc = ds.Tables[0].Rows;
            int count = drc.Count;
            
            for (int i = 0; i < count; i++)
            {
                PowerNameSettingPojo pojo = new PowerNameSettingPojo();
                pojo.id= (int)drc[i]["id"];
                pojo.ammeterNo= (int)drc[i]["ammeterNo"];
                pojo.ammeterName = (string)drc[i]["ammeterName"];
                list.Add(pojo);
            }
            {
                PowerNameSettingPojo pojo = new PowerNameSettingPojo();
                //pojo.id = 1;
                //pojo.ammeterNo = 2;
                //pojo.ammeterName = "3";

                //Button btn= new Button();
                //btn.Text = "添加";
                //pojo.button = btn;
                list.Add(pojo);
            }
            dataGridView1.DataSource = list;
            //设置数据表格上显示的列标题
            dataGridView1.Columns[0].HeaderText = "序号";
            dataGridView1.Columns[1].HeaderText = "编号";
            dataGridView1.Columns[2].HeaderText = "名称";
            //设置数据表格为只读
            dataGridView1.ReadOnly = false;
            dataGridView1.Columns[0].ReadOnly = true;
            dataGridView1.Columns[1].ReadOnly = true;
            
            dataGridView1.EditMode = DataGridViewEditMode.EditOnF2;   //点击编辑单元格
                                                                      //不允许添加行
            dataGridView1.AllowUserToAddRows = true;
            //背景为白色
            dataGridView1.BackgroundColor = Color.White;
            //只允许选中单行
            dataGridView1.MultiSelect = false;
            //整行选中
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }
        private void QueryAllCourse()
        {
            //数据库连接串
            string connStr = "Data Source=.;Initial Catalog=hohd;User ID=sa;Password=123456";
            //创建SqlConnection的实例
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(connStr);
                //打开数据库
                conn.Open();
                string sql = "select * from st_power_name";
                //创建SqlDataAdapter类的对象
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                //创建DataSet类的对象
                DataSet ds = new DataSet();
                //使用SqlDataAdapter对象sda将查新结果填充到DataSet对象ds中
                sda.Fill(ds);
                //设置表格控件的DataSource属性
                dataGridView1.DataSource = ds.Tables[0];
                //设置数据表格上显示的列标题
                dataGridView1.Columns[0].HeaderText = "序号";
                dataGridView1.Columns[1].HeaderText = "编号";
                dataGridView1.Columns[2].HeaderText = "名称";
                //设置数据表格为只读
                dataGridView1.ReadOnly = false;
                dataGridView1.EditMode = DataGridViewEditMode.EditOnF2;   //点击编辑单元格
                //不允许添加行
                dataGridView1.AllowUserToAddRows = true;
                //背景为白色
                dataGridView1.BackgroundColor = Color.White;
                //只允许选中单行
                dataGridView1.MultiSelect = false;
                //整行选中
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                DataGridViewRow row = new DataGridViewRow();
                int index = dataGridView1.Rows.Add(row);

                dataGridView1.Rows[index].Cells[0].Value = "张三-1";

                dataGridView1.Rows[index].Cells[1].Value = "张三-2";

                dataGridView1.Rows[index].Cells[2].Value = "张三-3";

                //dataGridView1.Rows[index].Cells[5].Value = 

            }
            catch (Exception ex)
            {
                MessageBox.Show("查询错误！" + ex.Message);
            }
            finally
            {
                if (conn != null)
                {
                    //关闭数据库连接
                    conn.Close();
                }
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            PowerNameSettingPojo pojo=list[list.Count - 1];
            if (set.Contains(pojo.ammeterNo))
            {
                MessageBox.Show("重复的号码");
                return;
            }
            PowerNameSettingPojo newP = new PowerNameSettingPojo();
            newP.ammeterNo = pojo.ammeterNo;
            newP.ammeterName = pojo.ammeterName;
            list.Insert(list.Count - 1, newP);
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = list;

            //设置数据表格上显示的列标题
            dataGridView1.Columns[0].HeaderText = "序号";
            dataGridView1.Columns[1].HeaderText = "编号";
            dataGridView1.Columns[2].HeaderText = "名称";
            //设置数据表格为只读
            dataGridView1.ReadOnly = false;
            dataGridView1.Columns[0].ReadOnly = true;
            dataGridView1.Columns[1].ReadOnly = true;

            dataGridView1.EditMode = DataGridViewEditMode.EditOnF2;   //点击编辑单元格
                                                                      //不允许添加行
            dataGridView1.AllowUserToAddRows = true;
            //背景为白色
            dataGridView1.BackgroundColor = Color.White;
            //只允许选中单行
            dataGridView1.MultiSelect = false;
            //整行选中
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            int count = dataGridView1.Rows.Count;
            dataGridView1.Rows[count - 1].Cells[1].ReadOnly = false;
            dataGridView1.Rows[count - 1].Cells[2].ReadOnly = false;
            //Console.WriteLine();
        }
    }
}
